package com.wushijia.highconcurrency.future;

import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

public class FutureTaskDemo1 {

  static class CalculationCallable implements Callable<Integer> {

    private int x;
    private int y;

    public CalculationCallable(int x, int y) {
      this.x = x;
      this.y = y;
    }

    @Override
    public Integer call() throws Exception {
      System.out.println("begin call: " + new Date());
      TimeUnit.SECONDS.sleep(2);
      return x * y;
    }
  }

  public static void main(String[] args) throws ExecutionException, InterruptedException {
    CalculationCallable callable = new CalculationCallable(10, 20);
    FutureTask<Integer> futureTask = new FutureTask<>(callable);
    new Thread(futureTask).start();
    Thread.sleep(2000);
    Integer result = futureTask.get();
    System.out.println("end call: " + new Date());
    System.out.println("result: " + result);
  }
}
